package com.brandonflowers.hiveswfaddressdemo.states
{
	import com.brandonflowers.hiveswfaddressdemo.data.HiveDemoData;
	import flash.display.Shape;
	
	import org.hive.managers.plugin.LayoutManager;
	import org.hive.managers.plugin.LoadManager;
	import org.hive.managers.StateManager;
	import org.hive.state.*
	import org.hive.sequence.*
	import org.hive.data.LoaderData;
	import org.hive.data.LoaderSet;
	import org.hive.events.LoaderSetEvent;
	
	
	import com.brandonflowers.hiveswfaddressdemo.states.*;
	import com.brandonflowers.hiveswfaddressdemo.views.NavView;
	
	public class MainState extends State implements IState
	{
		
		private var nav:NavView;
		
		public override function create():ActionSequence
		{
			//________________________________________________________________________ INIT MANAGERS
			LayoutManager.init();
			LoadManager.init();
			
			preloadNavGraphics();		

			//________________________________________________________________________ SEQUENCE
			var createSequence:ActionSequence = new ActionSequence( "Main::createSequence" );
			
			// the NavView will set the swfAddress to home and call the HomeState	
			
			//createSequence.push( new SequenceFunction(StateManager, StateManager.switchState, new HomeState()) );
			
			return createSequence;
		}
		
		private function preloadNavGraphics()
		{
			var bulkloadSequence:ActionSequence = new ActionSequence( "Preloader Sequence" );
			
			LoadManager.add
			(
				// NAV
				
				new LoaderData( "navAboutRollOver", contentList.getContentByName( 'navAboutRollOver' )  ),
				new LoaderData( "navAboutRollOut", contentList.getContentByName( 'navAboutRollOut' )  ),
				new LoaderData( "navContactRollOut", contentList.getContentByName( 'navContactRollOut' )  ),
				new LoaderData( "navContactRollOver", contentList.getContentByName( 'navContactRollOver' )  ),	
				new LoaderData( "navHomeRollOut", contentList.getContentByName( 'navHomeRollOut' )  ),	
				new LoaderData( "navHomeRollOver", contentList.getContentByName( 'navHomeRollOver' )  )		
				
			);
						
			
			// LOADERSET
			var bulkLoaderSet:LoaderSet = new LoaderSet(   "Bulk LoaderSet", 
															10,
															"navAboutRollOver",
															"navAboutRollOut",
															"navContactRollOut",
															"navContactRollOver",	
															"navHomeRollOut",
															"navHomeRollOver"
															);												
															
			// EVENTS
			bulkLoaderSet.addEventListener( LoaderSetEvent.ON_CHANGE, contentLoadProgressHandler, false, 0, true );
			bulkLoaderSet.addEventListener( LoaderSetEvent.ON_COMPLETE, completeHandler, false, 0, true );
			bulkLoaderSet.addEventListener( LoaderSetEvent.ON_ERROR, function(e:LoaderSetEvent){ trace ("error: " + e.error.toString()) }, false, 0, true );												
			
			//
			LoadManager.add(bulkLoaderSet);	
														
			
			//trace ('test: ' +  contentList.getContentByName( 'maydwellTitleLarge' ) );
			
			bulkloadSequence.push( bulkLoaderSet );
			
			StateManager.playSequence(bulkloadSequence);
		}
		
		private function contentLoadProgressHandler( e:LoaderSetEvent ):void
		{
			// you feed a preloader animation here if you wanted to...
			//loading.feedPreloader(e.percent);
		}
		
		
		private function completeHandler(e:LoaderSetEvent=null )
		{
			var addNavSequence:ActionSequence = new ActionSequence("addNavSequence");
			
			nav = new NavView();
			nav.alpha = 0;
									
			addNavSequence.push( {scope: nav, time:1, alpha:1} );
									
			StateManager.playSequence(addNavSequence);
		}
		
		public override function close():CloseSequence
		{
			var closeSequence:CloseSequence = new CloseSequence( "Main::closeSequence" );
			
			return closeSequence;
		}
		
	}
	
}
